import java.security.AllPermission;
import java.util.LinkedList;

public class Engine
{

	public void Mim(SearchStrategy usedStrategy, boolean visualize)
	{
		// defining operators and adding them in a Linked list
		LinkedList<String> operators = new LinkedList<String>();
		operators.add("Up");
		operators.add("Down");
		operators.add("Left");
		operators.add("Right");

		// creating the board
		Board initialBoard = new Board();
		initialBoard.createBoard();
		initialBoard.printBoard();

		// define initial state
		State initialState = new State(initialBoard);

		// initializing the goalTest
		boolean goalTest = false;

		// initialize the path cost
		int pathCost = 0;

		// initialize the problem
		Problem myProblem = new Problem(operators, initialState, goalTest,
				pathCost);
		// initialize the generic search
		GenericSearch generalSearch = new GenericSearch(myProblem, usedStrategy);

		Node solution = generalSearch.gnSearch();

		System.out.println("Done with the Search!");
		if (solution != null)
		{
			solution.setScore(solution.getLevel());
			pathCost = solution.getScore();
			wayToSol(solution, visualize);
			System.out.println("Path cost :" + pathCost);
			System.out.println("Move Block " + "0" + " to the " + "Right");
		}
		else
		{
			System.out.println("Sorry there is no solution found");
		}
		System.out.println("Number of the Nodes visited during the search "
				+ generalSearch.allNodes);

	}

	

	public void wayToSol(Node solution, boolean visualize)
	{

		if (solution.getParent() == null)
			return;
		wayToSol(solution.getParent(), visualize);
		System.out.println("Move Block "
				+ solution.getBlocktoBeMoved().getBlockNumber() + " to the "
				+ solution.getCurrentMovement());
		if (visualize)
		{
			solution.getCurrentState().getCurrentBoard().printBoard();
		}

	}

	public static void main(String[] args)
	{
		Engine engine = new Engine();

		// choose the strategy used
		SearchStrategy usedStrategy = new SearchStrategy("AS1");
		engine.Mim(usedStrategy, true);
	}

}
